﻿using System.Xml.Serialization;
using System.IO;

namespace PluginAmazon
{
    /// <summary>
    /// Wrapper für die Zugangsdaten zum AmazonWebService.
    /// </summary>
    public class AmazonKey
    {
        string accessKeyID;
        /// <summary>
        /// Access Key ID des Amazon WebService
        /// </summary>
        public string AccessKeyID
        {
            get { return accessKeyID; }
            set { accessKeyID = value; }
        }
        string secretAccessKey;

        /// <summary>
        /// Secret Access Key des Amazon WebService
        /// </summary>
        public string SecretAccessKey
        {
            get { return secretAccessKey; }
            set { secretAccessKey = value; }
        }

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="AccessKeyID">AccessKeyID des Amazon WebService</param>
        /// <param name="SecretAccessKey">SecretAccessKey des Amazon WebService</param>
        public AmazonKey(string AccessKeyID, string SecretAccessKey)
        {
            this.AccessKeyID = AccessKeyID;
            this.SecretAccessKey = SecretAccessKey;
        }

        /// <summary>
        /// Leerer Konstruktor, nur zur Serialisierung notwendig.
        /// </summary>
        public AmazonKey()
        {
        }

        /// <summary>
        /// Deserialisiert eine Datei und gibt das AmazonKey-Objekt zurück
        /// </summary>
        /// <param name="Filename">Datei, die deserialisiert werden soll.</param>
        /// <returns>AmazonKey-Paar, dass aus der angegebenen Datei geladen wurde.</returns>
        public static AmazonKey Deserialize(string Filename)
        {
            XmlSerializer ser = new XmlSerializer(typeof(AmazonKey));
            StreamReader sr = new StreamReader(Filename);
            AmazonKey retVal = (AmazonKey)ser.Deserialize(sr);
            sr.Close();
            return retVal;
        }

        /// <summary>
        /// Serialisiert das Objekt in eine XML-Datei.
        /// </summary>
        /// <param name="Filename">Speicherort der XML-Datei.</param>
        public void Serialize(string Filename)
        {
            XmlSerializer ser = new XmlSerializer(typeof(AmazonKey));
            FileStream str = new FileStream(Filename, FileMode.Create);
            ser.Serialize(str, this);
            str.Close();
        }

    }
}
